<?php


namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Admin\Traits\AdminTrait;
use App\Http\Requests\Admin\BaseRequest;
use App\Models\Admin\AdminDev;
use App\Models\Admin\AdminDevSku;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class DevController extends AdminController
{
    use AdminTrait;

    public $msg=[
        'name.unique'=>'名称已存在',
        'spu.unique'=>'spu编码已存在',
        'sku.unique'=>'sku编码已存在',
        'model.unique'=>'型号已存在',
    ];

    protected $status_list=[
        '1'=>'上架',
        '0'=>'下架',
    ];

    protected $inventory_list=[
        '1'=>'开启库存',
        '0'=>'关闭库存'
    ];

    public function devList(BaseRequest $request)
    {
        $input=$request->all();
        //过滤数据
        $this->vali_rules['name']='string';
        $this->vali_rules['status']='in:0,1';
        $validator = Validator::make($input, $this->vali_rules,$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $this->getPageAndSize($input);
        $list=AdminDev::getList($input,$this->skip,$this->size);
        $count=AdminDev::getCount($input);
        $data=[
            'list'=>$list,
            'count'=>$count,
            'status_list'=>$this->status_list,
            'inventory_list'=>$this->inventory_list,
        ];
        return  $this->success($data);
    }

    public function addDev(BaseRequest $request)
    {
        $input=$request->except('sign','timeStamp');
        $rules=[
            'name'=>[
                'bail','required',
                Rule::unique('admin_dev','name')
            ],
            'spu'=>[
                Rule::unique('admin_dev','spu')
            ],
            'detail'=>'',
            'status'=>'required|in:0,1',
            'sku_list'=>'required',
            'pic'=>'',
            'pics'=>'',
        ];
        $validator = Validator::make($input, $rules,$this->msg+$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $list=json_decode($input['sku_list'],true);
        if(!is_array($list))
        {
            return $this->error(['-1','规格格式有误']);
        }
        foreach ($list as $v)
        {
            $rules=[
                'model'=>[
                    'bail','required',
                    Rule::unique('admin_dev_sku','model')
                ],
                'sku'=>[
                    Rule::unique('admin_dev_sku','sku')
                ],
                'price_original'=>'required|numeric',
                'status'=>'required|in:0,1',
                'price_selling'=>'required|numeric',
                'use_inventory'=>'required|in:0,1',
                'inventory'=>'numeric',
            ];
            $validator = Validator::make($v, $rules,$this->msg+$this->vali_msg);
            if($validator->fails())
                return $this->error(['-1',$validator->errors()->first()]);
        }
        DB::beginTransaction();
        try {
            $dev_data=$input;
            unset($dev_data['sku_list']);
            unset($dev_data['img_list']);
            $info=AdminDev::create($dev_data);
            AdminDev::addLog($info->toArray(),[],1);
            $dev_id=$info->id;
            foreach ($list as $v)
            {
                $v['dev_id']=$dev_id;
                AdminDevSku::create($v);
            }
            DB::commit();
            return  $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return $this->error();
        }
    }

    public function editDev(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'id'=>[
                'bail','required',
                Rule::exists('admin_dev','id'),
            ],
            'name'=>[
                'required',
                Rule::unique('admin_dev','name')->ignore($input['id'] ?? 0)
            ],
            'spu'=>[
                Rule::unique('admin_dev','spu')->ignore($input['id'] ?? 0)
            ],
            'status'=>'required|in:0,1',
            'sku_list'=>'required',
            'img_list'=>'',
            'detail'=>'',
            'pic'=>'',
            'pics'=>'',

        ];
        $validator = Validator::make($input, $rules,$this->msg+$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $list=json_decode($input['sku_list'],true);
        if(!is_array($list))
        {
            return $this->error(['-1','规格格式有误']);
        }
        foreach ($list as $v)
        {
            $rules=[
                'model'=>[
                    'bail','required',
                    Rule::unique('admin_dev_sku','model')->ignore($v['id']?? 0)
                ],
                'sku'=>[
                    Rule::unique('admin_dev_sku','sku')->ignore($v['id']?? 0)
                ],
                'price_original'=>'required|numeric',
                'status'=>'required|in:0,1',
                'price_selling'=>'required|numeric',
                'use_inventory'=>'required|in:0,1',
                'inventory'=>'numeric',
                'id'=>[
                    Rule::exists('admin_dev_sku')->where('dev_id',$input['id']?? 0)
                ]
            ];
            $validator = Validator::make($v, $rules,$this->msg+$this->vali_msg);
            if($validator->fails())
                return $this->error(['-1',$validator->errors()->first()]);
        }



        DB::beginTransaction();
        try {
            $info=AdminDev::find($input['id']);
            $perInfo = $info->getOriginal();//获取修改之前的字段
            $info->name=$input['name'];
            $info->spu=$input['spu']?? '';
            $info->status=$input['status'];
            $info->detail=$input['detail']?? '';
            $info->pic=$input['pic']?? '';
            $info->pics=$input['pics']?? '';
            $info->save();
            $nextInfo = $info->getChanges();//获取修改之后的字段
            AdminDev::addLog($perInfo,$nextInfo,2);

            foreach ($list as $v)
            {
                if(!empty($v['id']))
                {
                    AdminDevSku::where('id',$v['id'])->update($v);
                }
                else
                {
                    $v['dev_id']=$input['id'];
                    AdminDevSku::create($v);
                }
            }
            $is_status_1=AdminDevSku::where('dev_id',$input['id'])->where('status',1)->count();
            if(!$is_status_1)
            {
                AdminDev::where('id',$input['id'])->update(['status'=>0]);
            }
            DB::commit();
            return  $this->success();
        }
        catch (\Exception $exception)
        {
            DB::rollBack();
            return $this->error();
        }
    }

    public function devDetail(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'id'=>[
                'bail','required',
                Rule::exists('admin_dev','id'),
            ],
        ];
        $validator = Validator::make($input, $rules,$this->msg+$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        $info=AdminDev::getDetail($input['id']);
        $data=[
            'info'=>$info,
            'status_list'=>$this->status_list,
            'inventory_list'=>$this->inventory_list,
        ];
        return $this->success($data);
    }

    public function exportDev(BaseRequest $request)
    {
        $input=$request->all();
        $rules=[
            'dev_ids'=>''
        ];
        $validator = Validator::make($input, $rules,$this->msg+$this->vali_msg);
        if($validator->fails())
            return $this->error(['-1',$validator->errors()->first()]);
        //获取列表

    }
}
